﻿function DateChangeByWeek(value) {
    var sDate;
    var eDate;
    var arrS;
    var arrE;
    if (value == -1) {
        arrS = document.getElementById("s_startDate").innerHTML.split("-");
        arrE = document.getElementById("s_endDate").innerHTML.split("-");
        sDate = (new Date(arrS[0], arrS[1] - 1, arrS[2])).addDays(-7);
        eDate = (new Date(arrE[0], arrE[1] - 1, arrE[2])).addDays(-7);
        document.getElementById("s_startDate").innerHTML = sDate.Format("yyyy-MM-dd");
        document.getElementById("s_endDate").innerHTML = eDate.Format("yyyy-MM-dd");
        if (CompareDate(new Date(eDate), new Date())) {
            document.getElementById("next").style.display = "";
        }
        tree.load({
            datetime: sDate.Format("yyyy-MM-dd")
        });
    } else {
        arrS = document.getElementById("s_startDate").innerHTML.split("-");
        arrE = document.getElementById("s_endDate").innerHTML.split("-");
        sDate = (new Date(arrS[0], arrS[1] - 1, arrS[2])).addDays(7);
        eDate = (new Date(arrE[0], arrE[1] - 1, arrE[2])).addDays(7);
        document.getElementById("s_startDate").innerHTML = sDate.Format("yyyy-MM-dd");
        document.getElementById("s_endDate").innerHTML = eDate.Format("yyyy-MM-dd");
        if (CompareDate(new Date(), (new Date(eDate)).addDays(1))) {
            document.getElementById("next").style.display = "";
        }
        tree.load({
            datetime: sDate.Format("yyyy-MM-dd")
        });
    }
}

function DateChangeByMonth(value) {
    var sDate;
    var eDate;
    var arrS;
    if (value == -1) {
        arrS = document.getElementById("s_startDate").innerHTML.split("-");
        sDate = (new Date(arrS[0], arrS[1] - 1, arrS[2])).addMonth(-1);
        eDate = (new Date(sDate)).addMonth(1).addDays(-1);
        document.getElementById("s_startDate").innerHTML = sDate.Format("yyyy-MM-dd");
        document.getElementById("s_endDate").innerHTML = eDate.Format("yyyy-MM-dd");
        if (CompareDate(new Date(eDate), new Date())) {
            document.getElementById("next").style.display = "";
        }
        tree.load({
            datetime: sDate.Format("yyyy-MM-dd")
        });
    } else {
        arrS = document.getElementById("s_startDate").innerHTML.split("-");
        sDate = (new Date(arrS[0], arrS[1] - 1, arrS[2])).addMonth(1);
        eDate = new Date(sDate).addMonth(1).addDays(-1);
        document.getElementById("s_startDate").innerHTML = sDate.Format("yyyy-MM-dd");
        document.getElementById("s_endDate").innerHTML = eDate.Format("yyyy-MM-dd");
        if (CompareDate(new Date(), (new Date(eDate)).addDays(1))) {
            document.getElementById("next").style.display = "";
        }
        tree.load({
            datetime: sDate.Format("yyyy-MM-dd")
        });
    }
}

function DateChangeByQuarter(value) {
    var sDate;
    var eDate;
    var arrS;
    if (value == -1) {
        arrS = document.getElementById("s_startDate").innerHTML.split("-");
        sDate = (new Date(arrS[0], arrS[1] - 1, arrS[2])).addMonth(-3);
        eDate = (new Date(sDate)).addMonth(3).addDays(-1);
        document.getElementById("s_startDate").innerHTML = sDate.Format("yyyy-MM-dd");
        document.getElementById("s_endDate").innerHTML = eDate.Format("yyyy-MM-dd");
        if (CompareDate(new Date(eDate), new Date())) {            
            document.getElementById("next").style.display = "";
        }
        tree.load({
            datetime: sDate.Format("yyyy-MM-dd")
        });
    } else {
        arrS = document.getElementById("s_startDate").innerHTML.split("-");
        sDate = (new Date(arrS[0], arrS[1] - 1, arrS[2])).addMonth(3);
        eDate = new Date(sDate).addMonth(3).addDays(-1);
        document.getElementById("s_startDate").innerHTML = sDate.Format("yyyy-MM-dd");
        document.getElementById("s_endDate").innerHTML = eDate.Format("yyyy-MM-dd");
        if (CompareDate(new Date(), (new Date(eDate)).addDays(1))) {
            document.getElementById("next").style.display = "";
        }
        tree.load({
            datetime: sDate.Format("yyyy-MM-dd")
        });
    }
}

/*启动行编辑
//var j = 0;
//setTimeout(getRows, 200);

//function getRows() {
//    var tree = mini.get("treegrid");
//    if (tree.data.length > 0 && j < 10) {
//        for (var i = 0; i < tree.data.length; i++) {
//            var row = tree.data[i];
//            tree.beginEditRow(row);
//        }
//    }
//    else {
//        setTimeout(getRows, 200);
//        j++;
//    }
//}*/
//DateChangeByQuarter,DateChangeByMonth

//批量汇报保存
function SaveReportData(type) {
    
    var tree = mini.get("treegrid");
    var rows = tree.getChanges();
    if (rows.length <= 0) {
        alert("没有可保存的项！");
        return;
    }
    var task_ids = [];
    var target_descriptions = [];
    var work_reports = [];
    var progresses = [];
    var line_ids = [];
    var reportStatus = [];
    var startDate = document.getElementById("s_startDate").innerHTML;
    var endDate = document.getElementById("s_endDate").innerHTML;
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];
        task_ids.push(row.task_id_);
        row.target_description_ == null ? target_descriptions.push("") : target_descriptions.push(row.target_description_);
        row.work_report_ == null ? work_reports.push("") : work_reports.push(row.work_report_);
        row.line_id_ == null ? line_ids.push("") : line_ids.push(row.line_id_);
        progresses.push(row.progress_);
        row.reportStatus == null ? reportStatus.push("") : reportStatus.push(row.reportStatus);

    }
    
    $.ajax({
        url: "/AJAX/AjaxService.aspx?method=SaveReportTask",
        cache: false,
        type: "post",
        data: {
            taskID_: task_ids,
            target_description_: target_descriptions,
            work_report_: work_reports,
            progress_: progresses,
            line_id_: line_ids,
            reportStatus: reportStatus,
            start_date: startDate,
            end_date: endDate,
            type: type
        },
        success: function (text) {
            tree.reload();
        }
    });
}
function selectValid() {
    var tree = mini.get("treegrid");
    var rows = tree.getChanges();
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];
        var select = tree.getSelected();
        if (row == select)
            return;
        if (row.target_description_ == null || row.target_description_.length <= 0) {
            alert("请填写任务【" + row.task_name_ + "】本周目标！");
            tree.deselect(select, false);
            tree.setSelected(row);
            return;
        }
        if (row.work_report_ == null || row.work_report_.length <= 0) {
            alert("请填写任务【" + row.task_name_ + "】本周汇报情况！");
            tree.deselect(select, false);
            tree.setSelected(row);
            return;
        }
    }
}
        